package com.fiume.travel.service.impl;

import com.fiume.travel.mapper.SellerMapper;
import com.fiume.travel.pojo.Seller;
import com.fiume.travel.pojo.SellerExample;
import com.fiume.travel.service.SellerService;
import com.fiume.travel.utils.BeanConv;
import com.fiume.travel.utils.EmptyUtil;
import com.fiume.travel.utils.MD5Coder;
import com.fiume.travel.vo.SellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * Description: 商家模块实现类
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-22 20:30
 * @see com.fiume.travel.service.impl
 */
@Service
public class SellerServiceImpl implements SellerService {

    @Resource
    private SellerMapper sellerMapper;

    @Resource
    HttpSession session;

    /**
     * 商家用户注册
     *
     * @param sellerVo 商家对象
     * @return 注册是否成功
     */
    @Override
    public Boolean registerSeller(SellerVo sellerVo) {
        Seller seller = BeanConv.toBean(sellerVo, Seller.class);
        if (seller==null || seller.getPassword() == null){
            return null;
        }
        //判断该用户名是否已被注册
        //组装条件
        SellerExample example = new SellerExample();
        example.createCriteria()
                .andUsernameEqualTo(sellerVo.getUsername());
        //查询list结果集
        List<Seller> sellers = sellerMapper.selectByExample(example);
        //转换VO
        List<SellerVo> list = BeanConv.toBeanList(sellers, SellerVo.class);
        if (list.size()==1){
            return false;
        }
        //加密密码
        seller.setPassword(MD5Coder.md5Encode(seller.getPassword()));
        int flag = sellerMapper.insert(seller);
        return flag>0;
    }

    /**
     * 商家登录
     *
     * @param sellerVo 商家对象
     * @return 登录成功后返回
     */
    @Override
    public SellerVo loginSeller(SellerVo sellerVo) {
        //组装条件
        SellerExample example = new SellerExample();
        example.createCriteria()
                .andUsernameEqualTo(sellerVo.getUsername())
                .andPasswordEqualTo(MD5Coder.md5Encode(sellerVo.getPassword()));
        //查询list结果集
        List<Seller> sellers = sellerMapper.selectByExample(example);
        //转换VO
        List<SellerVo> list = BeanConv.toBeanList(sellers, SellerVo.class);
        //判断结果
        if (list.size()==1){
            SellerVo sellerVoResult = list.get(0);
            //放入会话
            String sessionId = session.getId();
            sellerVoResult.setSessionId(sessionId);
            // 将登录的商家信息保存到session中
            session.setAttribute(sessionId,sellerVoResult);
            return sellerVoResult;
        }
        return null;
    }

    /**
     * 商家退出
     */
    @Override
    public void loginOutSeller() {
        //从会话中删除当前对象
        session.removeAttribute(session.getId());
    }

    /**
     * 商家是否登录
     *
     * @return 商家是否登录
     */
    @Override
    public Boolean isLogin() {
        //根据当前用户是否存在判断登录状态
        Object sessionObject = session.getAttribute(session.getId());
        return !EmptyUtil.isNullOrEmpty(sessionObject);
    }

    /**
     * 按照商家Id查询商家用户
     * 查询线路详情时使用
     *
     * @param sellerVo 商家对象
     * @return 商家vo对象
     */
    @Override
    public SellerVo findSellerVoById(SellerVo sellerVo) {
        return BeanConv.toBean(sellerMapper.selectByPrimaryKey(sellerVo.getId()),SellerVo.class);
    }
}
